# -*- encoding: utf-8 -*-
import sys
import pymysql


class DBUtils():

    def __init__(self, host, user, password, database ):
        self.con = pymysql.connect(host=host,
                               port=3306,
                               user=user,
                               password=password,
                               database=database,
                               charset='utf8')
        self.cur = self.con.cursor()

    def insert_all(self, sql, products):
        try:
            self.cur.executemany(sql,products)
            self.con.commit()
        except:
            self.con.rollback()
        finally:
            self.cur.close()
            self.con.close()



    def select(self, sql, args):

        '''暂时省略参数构造'''

        self.cur.execute(sql)
        # print self.cur.description
        result = self.cur.fetchall()
        data_dict = []
        for field in self.cur.description:
            data_dict.append(field[0])
        # print data_dict

        self.cur.close()
        self.con.close()

        return data_dict

    def select1(self, sql, args):

        '''暂时省略参数构造'''

        self.cur.execute(sql)
        # print self.cur.description
        result = self.cur.fetchone()


        self.cur.close()
        self.con.close()

        return result

    def insert_one(self, file_name, file_md5):
        try:
            sql_str = "INSERT INTO t_forward_file (Ffile_name, Ffile_md5)",+ " VALUES ('%s', '%s')" % (file_name, file_md5)
            self.cur.execute(sql_str)
            self.con.commit()
        except:
            self.con.rollback()

            raise
        finally:
            self.cur.close()
            self.con.close()